<?php

/**
 *    营收控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class Stat_revenueApp extends BackendApp
{
    /**
     *    营收统计
     *
     *    @author    Garbin
     *    @return    void
     */

    function order()
    {
        $this->assign('admin', $this->visitor->get());

        $ms =& ms();
        //$this->assign('new', $ms->pm->check_new($this->visitor->get('user_id')));

        $order_mod =& m('order');
        $order_all = $order_mod->getOne("SELECT SUM(order_amount) FROM " . DB_PREFIX . "order WHERE status IN (20,30,40)");
        $order_undue = $order_mod->getOne("SELECT SUM(order_amount) FROM " . DB_PREFIX . "order WHERE status IN (11)");
        $order_unrefund = $order_mod->getOne("SELECT SUM(order_amount) FROM `ecm_order` LEFT JOIN `ecm_refund` ON ecm_order.order_id = ecm_refund.order_id WHERE ecm_refund.status IN('WAIT_SELLER_AGREE','WAIT_SELLER_CONFIRM','WAIT_ADMIN_AGREE')");
        $order_refund = $order_mod->getOne("SELECT SUM(order_amount) FROM `ecm_order` LEFT JOIN `ecm_refund` ON ecm_order.order_id = ecm_refund.order_id WHERE ecm_refund.status IN('SUCCESS')");
        $res_refund = $order_mod->getOne("SELECT COUNT(*) FROM `ecm_order` RIGHT JOIN `ecm_refund` ON ecm_order.order_id = ecm_refund.order_id");
        $res_all = $order_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status IN (20,30,40)");
        $order_chance = round($res_refund/$res_all,2);
        $stat = array(
            'all' => $order_all,
            'undue' => $order_undue,
            'unrefund' => $order_unrefund,
            'refund' => $order_refund ? $order_refund : 0,
            'chance' => $order_chance
        );
        $this->assign('revenue', $stat);

        // 统计信息
//        $stats = $this->_get_stats();
//        $this->assign('stats', $stats);

        // 系统信息
        $sys_info = $this->_get_sys_info();
        $this->assign('sys_info', $sys_info);

        // 提示信息
        $remind_info = $this->_get_remind_info();
        $this->assign('remind_info', $remind_info);
        $dangerous_apps  = false;
        if (is_file(ROOT_PATH . '/initdata/index.php'))
        {
            $dangerous_apps[] = Lang::get('dangerous_initdata');
        }
        if (is_file(ROOT_PATH . '/integrate/index.php'))
        {
            $dangerous_apps[] = Lang::get('dangerous_integrate');
        }

        $this->assign('dangerous_apps', $dangerous_apps);

        // 当前语言
        $this->assign('cur_lang', LANG);

        $this->_update_store_state();
        $this->_update_site_information($stats, $sys_info);
        $this->display('stat_revenue.html');
    }

    /**
     *    关于我们页面
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function aboutus()
    {
        $this->headtag('<base target="_blank" />');
        $this->display('aboutus.html');
    }

    function _get_menu()
    {
        $menu = include(APP_ROOT . '/includes/menu.inc.php');

        return $menu;
    }

    function _get_news_in_a_week()
    {
        $a_week_ago = gmtime() - 7 * 24 * 3600;
        $user_mod =& m('member');
        return array(
            'new_user_qty'  => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "member WHERE reg_time > '$a_week_ago'"),
            'new_store_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "store WHERE add_time > '$a_week_ago' AND state = 1"),
            'new_apply_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "store WHERE add_time > '$a_week_ago' AND state = 0"),
            'new_goods_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods WHERE add_time > '$a_week_ago' AND if_show = 1 AND closed = 0"),
            'new_order_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE finished_time > '$a_week_ago' AND status = '" . ORDER_FINISHED . "'"),
            'new_order_amount' => $user_mod->getOne("SELECT SUM(order_amount) FROM " . DB_PREFIX . "order WHERE finished_time > '$a_week_ago' AND status = '" . ORDER_FINISHED . "'"),
        );
    }

    function _get_stats()
    {
        $user_mod =& m('member');
        return array(
            'user_qty'  => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "member"),
            'store_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "store WHERE state = 1"),
            'apply_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "store WHERE state = 0"),
            'goods_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods WHERE if_show = 1 AND closed = 0"),
            'order_qty' => $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_FINISHED . "'"),
            'order_amount' => $user_mod->getOne("SELECT SUM(order_amount) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_FINISHED . "'"),
            'admin_email' => $user_mod->getOne("SELECT email FROM " . DB_PREFIX . "member WHERE user_id = '1'"),
        );
    }
    
    /*
     * 获取待处理订单
     */
    function _get_shop_stats()
    {
        $user_mod =& m('member');
        $tixian_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "account_log WHERE type = '" . ACCOUNT_TYPE_TX . "' AND states = 70");
        $apply_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "store WHERE state = 0");
        $refund_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "refund WHERE ask_customer = 1");
        $total = $tixian_qty + $apply_qty + $refund_qty;
        return array(
            //待处理提现
            'tixian_qty'  => $tixian_qty,
            //待审核店铺
            'apply_qty' => $apply_qty,
            //待处理退款
            'refund_qty'=> $refund_qty,
            //合计
            'total'=>$total,
        );
    }
    
    /*
     * 获取商城商品信息
     */
    function _get_goods_stats()
    {
        $user_mod =& m('member');
        $goods_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods");
        $goods_on_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods WHERE if_show = 1 AND closed = 0");
        $goods_off_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods WHERE if_show = 0 AND closed = 0");
        $goods_close_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "goods WHERE closed = 1");
        return array(
            //总共商品数
            'goods_qty'  => $goods_qty,
            //上架的商品数
            'goods_on_qty' => $goods_on_qty,
            //待处理退款
            'goods_off_qty'=> $goods_off_qty,
            //禁售的商品数
            'goods_close_qty'=> $goods_close_qty,
        );
    }
    
    /*
     * 获取商城商品信息
     */
    function _get_order_stats()
    {
        $user_mod =& m('member');
        $total_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order");
        $pending_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_PENDING . "'");
        $accepted_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_ACCEPTED . "'");
        $finished_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_FINISHED . "'");
        $canceled_qty = $user_mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "order WHERE status = '" . ORDER_CANCELED . "'");
        return array(
            //总订单
            'total_qty'  => $total_qty,
            //待付款的订单
            'pending_qty' => $pending_qty,
            //待发货的订单
            'accepted_qty'=> $accepted_qty,
            //已完成的订单
            'finished_qty'=> $finished_qty,
            //已取消的订单
            'canceled_qty'=> $canceled_qty,
        );
    }

    function _get_sys_info()
    {
        $user_mod =& m('member');
        $filename = ROOT_PATH . '/data/install.lock';
        return array(
            'server_os'     => PHP_OS,
            'web_server'    => $_SERVER['SERVER_SOFTWARE'],
            'php_version'   => PHP_VERSION,
            'mysql_version' => $user_mod->db->version(),
            'ecmall_version'=>   '3.0 20171220' ,
            'install_date'  => file_exists($filename) ? date('Y-m-d', fileatime($filename)) : date('Y-m-d'),
        );
    }

    function _update_site_information($stats, $sys_info)
    {
        $update = array(
            'uniqueid'  => MALL_SITE_ID,
            'version'   => VERSION,
            'release'   => RELEASE,
            'php'       => PHP_VERSION,
            'mysql'     => $sys_info['mysql_version'],
            'charset'   => CHARSET,
            'url'       => SITE_URL,
        );
        $this->assign('uniqueid',base64_encode(MALL_SITE_ID));
        $update_time = 0;
        $update_file = ROOT_PATH . '/data/update_time.lock';
        if (file_exists($update_file))
        {
            $update_time = filemtime($update_file);
        }

        $timestamp = time();
        if(empty($update_time) || ($timestamp - $update_time > 3600 * 4))
        {
            touch($update_file);
            $stat_info = array();
            $stat_info['page_view']    = 1; // todo: no data
            $stat_info['order_amount'] = $stats['order_amount'];
            $stat_info['order_count']  = $stats['order_qty'];
            $stat_info['store_count']  = $stats['store_qty'];
            $stat_info['member_count'] = $stats['user_qty']; // differ from 1.1
            $stat_info['goods_count']  = $stats['goods_qty']; // differ from 1.1
            $stat_info['admin_last_login_time'] = local_date('Y-m-d H:i:s');
            $stat_info['admin_email'] = $stats['admin_email'];
            foreach($stat_info AS $key => $value)
            {
                $update[$key] = $value;
            }
        }

        $data = '';
        foreach($update as $key => $value)
        {
            $data .= $key.'='.rawurlencode($value).'&';
        }

        $this->assign('spt', 'ht'. 'tp:/' . '/e' .'cmal' . 'l.sho' . 'pe' . 'x.c' . 'n/sy' . 'stem'. '/ecm' . 'all' . '_in' . 'stal' . 'l.p' . 'hp?'.'update='.rawurlencode(base64_encode($data)).'&md5hash='.substr(md5($_SERVER['HTTP_USER_AGENT'].implode('', $update).$timestamp), 8, 8).'&timestamp='.$timestamp);
    }

    function clear_cache()
    {
        $cache_server =& cache_server();
        $cache_server->clear();
        $this->json_result('', Lang::get('clear_cache_ok'));
    }

    /* 更新店铺状态：过期的关闭 */
    function _update_store_state()
    {
        $store_mod =& m('store');
        $stores = $store_mod->find(array(
            'conditions' => "state = '" . STORE_OPEN . "' AND end_time > 0 AND end_time < '" . gmtime() . "'",
            'join'       => 'belongs_to_user',
            'fields'     => 'store_id, user_id, user_name, email',
        ));
        foreach ($stores as $store)
        {
            $subject = Lang::get('close_store_notice');
            $content = get_msg('toseller_store_closed_notify', array('reason' => Lang::get('close_reason')));
            /* 连接用户系统 */
            $ms =& ms();
            $ms->pm->send(MSG_SYSTEM, $store['user_id'], '', $content);

            $this->_mailto($store['email'], $subject, $content);
            $store_mod->edit($store['store_id'], array('state' => STORE_CLOSED, 'close_reason' => Lang::get('close_reason')));
        }
    }

    /* 取得提醒信息 */
    function _get_remind_info()
    {
        $remind_info = array();
        $mod =& m('store');

        // 地区
        $region_count = $mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "region WHERE parent_id = 0");
        $region_count == 0 && $remind_info[] = Lang::get('reminds.region');

        // 支付方式
        $filename = ROOT_PATH . '/data/payments.inc.php';
        $payments = array();
        if (file_exists($filename))
        {
            $payments = include_once $filename;
        }
        empty($payments) && $remind_info[] = Lang::get('reminds.payment');

        // 商品分类
        $gcate_count = $mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "gcategory WHERE store_id = 0 AND parent_id = 0 AND if_show = 1");
        $gcate_count == 0 && $remind_info[] = Lang::get('reminds.gcategory');

        // 店铺分类
        $scate_count = $mod->getOne("SELECT COUNT(*) FROM " . DB_PREFIX . "scategory WHERE parent_id = 0");
        $scate_count == 0 && $remind_info[] = Lang::get('reminds.scategory');

        return $remind_info;
    }
}
?>
